package com.example.liaotian.Fragment.UpFragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.liaotian.Adapter.ActAdapter;
import com.example.liaotian.Adapter.RecycAdapter;
import com.example.liaotian.Bean.Act;
import com.example.liaotian.Bean.Post;
import com.example.liaotian.R;
import com.example.liaotian.activity.PushActConActivity;
import com.example.liaotian.activity.PushTraContentActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**组织活动页面
 * A simple {@link Fragment} subclass.
 */
public class ActFragment extends Fragment {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Act> data;//存放帖子的集合
    private ActAdapter actAdapter;
    private FloatingActionButton add;


    public ActFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_act, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
//初始化Bmob
        Bmob.initialize(getActivity(),"320a371a14fd5a1f1820feeeeb04e264");
        initData();
        Refresh();//fragment加载页面的时候就刷新一次
        initClick();
    }

    private void initClick() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), PushActConActivity.class));
            }
        });
    }

    /**
     * 绑定数据
     */
    private void initView() {
        recyclerView = getView().findViewById(R.id.re);
        swipeRefreshLayout = getView().findViewById(R.id.sw);
        add = getView().findViewById(R.id.add);


    }
    private void initData() {
        //设置下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });

    }
    /**
     * 刷新的方法
     */
    private void Refresh(){
        BmobQuery<Act> postBmobQuery = new BmobQuery<>();
        postBmobQuery.setLimit(1000).order("-createdAt");

//        postBmobQuery.order("-createdAt");//按时间从现在到以前查询
//        postBmobQuery.setLimit(1000);//限制为1000
        postBmobQuery.findObjects(new FindListener<Act>() {
            @Override
            public void done(List<Act> list, BmobException e) {
                swipeRefreshLayout.setRefreshing(false);//拿到数据之后就停止刷新动画
                //如果没有异常,就把数据放进集合中，并把数据填进recyclerview
                if (e == null) {
                    data = list;//把拿到的数据放进我们自己的集合
                    actAdapter = new ActAdapter(getActivity(),data);//实例化适配器对象并放入数据
                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//设置recyclerview为上下滑动
                    recyclerView.setAdapter(actAdapter);//把填好数据的适配器放进recyclerview
                }else {//如果有异常弹出一个提示
                    swipeRefreshLayout.setRefreshing(false);
                    Toast.makeText(getActivity(),"获取数据失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
